package 线程创建.Runnable接口;

public class 龟兔赛跑 implements Runnable {
    private static String course;

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (sn(i)) {
                if (Thread.currentThread().getName().equals("兔子") && i % 50 == 0) {
                    try {
                        System.out.println(Thread.currentThread().getName());//输出线程名字
                        Thread.sleep((long) 0.001);//设置线程延迟
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "-->跑了" + i + "步");
            } else {
                System.out.println(course + "赢了");
                break;
            }

        }
    }

    public boolean sn(int s) {
        if (course != null) {

            return false;
        } else if (s == 100) {
            course = Thread.currentThread().getName();
        }
        return true;
    }

    public static void main(String[] args) {
        龟兔赛跑 t = new 龟兔赛跑();
        new Thread(t, "乌龟").start();//为线程命名
        new Thread(t, "兔子").start();
    }
}
